package alp.starcode.nudtportal.framework.util;

import org.springframework.util.StringUtils;

public class ZYStringUtil extends StringUtils {
    private ZYStringUtil() {
    }

    public static int[] getAgentIdAndClientId(String etClientId) {
        int[] arr = new int[2];
        String hexEtClientId = ZYStringUtil.fillLeft(Integer.toHexString(Integer.parseInt(etClientId)), '0', 6);
        arr[1] = Integer.parseInt(hexEtClientId.substring(0, 4), 16);
        arr[0] = Integer.parseInt(hexEtClientId.substring(4, 6), 16);
        return arr;
    }

    public static String cardSIDToOpCardSID(String cardSID) {
        String opCardSID = null;
        if (cardSID.length() >= 8) {
            String hexCardSID = cardSID.substring(6, 8)
                    + cardSID.substring(4, 6) + cardSID.substring(2, 4)
                    + cardSID.substring(0, 2);
            opCardSID = String.valueOf(Long.parseLong(hexCardSID, 16));
        }
        return opCardSID;
    }

    /**
     * 向左填充数据
     *
     * @param source 数据
     * @param fill   填充字符
     * @param len    需要填充长度
     */
    public static String fillLeft(String source, char fill, int len) {
        StringBuffer ret = new StringBuffer();
        if (null == source)
            ret.append("");
        assert source != null;
        if (source.length() > len) {
            ret.append(source);
        } else {
            int slen = source.length();
            while (ret.toString().length() + slen < len) {
                ret.append(fill);
            }
            ret.append(source);
        }
        return ret.toString();
    }

    /**
     * 向右填充数据
     *
     * @param source 数据
     * @param fill   填充字符
     * @param len    需要填充长度
     */
    public static String filRight(String source, char fill, int len) {
        StringBuffer ret = new StringBuffer();
        if (null == source)
            ret.append("");
        assert source != null;
        if (source.length() > len) {
            ret.append(source);
        } else {
            ret.append(source);
            while (ret.toString().length() < len) {
                ret.append(fill);
            }
        }
        return ret.toString();
    }
}
